﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 
using System.Windows.Forms;
using System.IO;
using Addin665.Frms;

namespace Addin665.Commands {
    class AddHeaderInfoCfgCmd : CommandBase {
        InputFrm _frm;

        public AddHeaderInfoCfgCmd() {
            this.Name = "AddHeaderInfoCfgCmd";
        }

        public override void OnClick() {
            base.OnClick();
            if (_frm == null || _frm.IsDisposed) {
                _frm = new InputFrm();
            }
            if (_frm.Visible) {
                return;
            }

            string fullPath = System.Reflection.Assembly.GetAssembly(typeof(Connect)).Location;
            string cfgFile = System.IO.Path.GetDirectoryName(fullPath) + @"\Data" + @"\AddHeaderInfo.txt";

            if (!Directory.Exists(System.IO.Path.GetDirectoryName(fullPath))) {
                System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(fullPath));
            }
            if (!File.Exists(cfgFile)) {
                using (System.IO.File.CreateText(cfgFile)) { }
            }
            else {
                _frm.InputText = CommonHelp.FileHelper.ReadFile(cfgFile);
            }

            DialogResult rst = _frm.ShowDialog(base.ApplicationObject.ActiveWindow as System.Windows.Forms.IWin32Window); 
            if (rst != DialogResult.OK) {
                return;
            }

            string txt = _frm.InputText;
            CommonHelp.FileHelper.WriteFile(cfgFile, txt);
        }
    }
}
